var api = {
	ajaxBaseUrl: '',
	ctl: '',
	int: function(a) {
		return "" == a || null == a ? 0 : (a += "", "px" == this.right(a, 2) ? (a = a.replace(/[^0-9.-]*/g, ""), "" == a && (a = "0"), a = parseInt(a)) : (a = a.replace(/[^0-9.-]*/g, ""), "" == a && (a = 0), a = parseInt(a)), a)
	},
	right: function(a, b) {
		if(this.isNull(a)) return "";
		a += "";
		var c = a.length;
		return c > b && (a = a.substring(c - b, c)), a
	},
	isNull: function(a) {
		var b = $.trim(a);
		return null == b ? !0 : "undefined" == b ? !0 : "" == b ? b === !1 || b === !0 ? !1 : !0 : !1
	},
	rep: function(str, rule, s) {
		return this.isNull(str) ? "" : (rule = rule.replace(/\$/g, "\\$"), eval("str.replace(/" + rule + "/g,s)"))
	},
	ajax: function(method, uri, datas, callback) {
		$.ajax({
			type: method,
			url: this.ajaxBaseUrl + uri,
			data: datas,
			success: function(res) {
				callback(res);
			}
		});
	},
	/**
	 * 
	 * 客服
	 * */
	service: function(a) {
		var b = "1" == a.open ? !0 : !1,
			c = a.data || "",
			d = "service-online",
			e = function() {
				var b, e, f, g, i, j, k, l, m, n, o, p, r, s, t, u, h = "";
				b = !c ? [] : $.parseJSON(c);
				for(e = 0; e < b.length; e++) i = unescape(b[e].name || ""), j = unescape(b[e].tel || ""), k = unescape(b[e].qq || ""), l = unescape(b[e].crmqq || ""), m = unescape(b[e].wangwang || ""), n = unescape(b[e].skype || ""), o = unescape(b[e].qrcode || ""), p = unescape(b[e].text || ""), "" != j && (j = '<dd class="tel">' + j + "</dd>"), "" != k && (k = '<dd class="qq"><a href="http://wpa.qq.com/msgrd?v=3&uin=' + k + '&site=qq&menu=yes" target="_blank"></a></dd>'), "" != l && (l = '<dd class="crmqq"><a href="' + l + '" target="_blank"></a></dd>'), "" != m && (m = '<dd class="wangwang"><a target="_blank" href="http://www.taobao.com/webww/ww.php?ver=3&touid=' + m + '&siteid=cntaobao&status=1&charset=utf-8"><img border="0" src="http://amos.alicdn.com/realonline.aw?v=2&uid=' + m + '&site=cntaobao&s=1&charset=utf-8" alt="点击这里给我发消息" /></a></dd>'), "" != n && (n = '<dd class="skype"><script type="text/javascript" src="http://skype.tom.com/script/skypeCheck40.js"></script><a href="skype:' + n + '?call" onclick="return skypeCheck();"><img src="http://mystatus.skype.com/bigclassic/' + n + '" style="border: none;" width="80" height="24" alt="My status" /></a></dd>'), "" != o && (o = '<dd class="qrcode"><img src="' + o + '" /></dd>'), "" != p && (p = '<dd class="skype">' + p + "</dd>"), g = "<dl><dt>" + i + "</dt>" + j + k + l + m + n + o + p + "</dl>", h = "" == g ? g : h + "" + g;
				f = '<div class="' + d + '"><a href="javascript:;" class="mini"></a><div class="listpanel">' + '<div class="heading"><a class="close" href="javascript:;"></a></div>' + '<div class="section">' + h + "</div>" + "</div></div>", $("body").append(f), r = $("div." + d).find(".mini"), s = $("div." + d).find(".listpanel"), t = $("div." + d).find("a.close"), "mini" == a.initType ? (r.show(), s.hide()) : (r.hide(), s.show()), r.click(function() {
					r.hide(), s.show("fast")
				}), t.click(function() {
					r.show("fast"), s.hide("fast")
				}), u = parseInt($("div." + d).css("top")), $(window).scroll(function() {
					$("div." + d).css("top", parseInt(document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop) + u)
				});
			};
		b && "" != c && e()
	},
	/**
	 * 
	 * 楼层
	 * */
	floor: function() {
		var $owFloor = $("#ow_shop_floor"),
			floorNum = 0;
		$("div[name='floor_goods']").each(function() {
			var $owShopFloor = $(this).parent().parent(),
				cateName = $owShopFloor.find("a[name='cate_name']").html();
			if($(this).find("ul:eq(0)").find("li").length == 0) {
				$owShopFloor.remove();
			} else {
				floorNum = floorNum + 1;
				$owShopFloor.attr("id", "floor" + floorNum);
				$owShopFloor.find(".floor-num").html(floorNum + "F");
			};
		});
		if($(".ow-shop-floor")) {
			var floorTop = 0,
				$elevatorFloor = $(".ow-elevator-floor");
			$.each($('.ow-shop-floor'), function(i, v) {
				var $f = $(v).find('.floor-num'),
					floorNum = $.trim($f.text()),
					floorName = $.trim($f.attr("floor_name"));
				if(floorName == '') {
					floorName = floorNum
				};
				var $a = $("<a class='smooth' href='javascript:;'><b class='floor-num'>" + floorNum + "</b><em class='floor-name'>" + floorName + "</em></a>");
				$elevatorFloor.append($a);
			});
			if($(".ow-shop-floor").length > 0) {
				floorTop = api.int($(".ow-shop-floor:eq(0)").offset().top);
			};
			$(window).scroll(function() {
				var scrollTop = $(window).scrollTop();
				if(scrollTop > floorTop - 250) {
					$(".ow-elevator").show(100, function() {
						$(".ow-elevator-floor").css({
							"-webkit-transform": "scale(1)",
							"-moz-transform": "scale(1)",
							"transform": "scale(1)",
							"opacity": "1"
						})
					}).css({
						"visibility": "visible"
					})
				} else {
					$(".ow-elevator-floor").css({
						"-webkit-transform": "scale(0.8)",
						"-moz-transform": "scale(0.8)",
						"transform": "scale(0.8)",
						"opacity": "0"
					});
					$(".ow-elevator").css({
						"visibility": "hidden"
					});
				}
				setTab();
			});
			var arr = [],
				fsOffset = 0;
			for(var i = 0; i < $(".ow-shop-floor").length; i++) {
				arr.push(api.int($(".ow-shop-floor").eq(i).offset().top))
			};
			$elevatorFloor.find("a").click(function() {
				var index = $elevatorFloor.find("a").index(this),
					scrollTop = arr[index] - 10;
				$("html,body").stop().animate({
					scrollTop: scrollTop + "px"
				}, 200);
			});
			$elevatorFloor.find("a.top").click(function() {
				$("html,body").stop().animate({
					scrollTop: "0px"
				}, 200)
			});

			function setTab() {
				var $floor = $(".ow-shop-floor");
				scrollTop = api.int($(window).scrollTop());
				for(var i = $floor.length - 1; i >= 0; i--) {
					if(scrollTop > (api.int($($floor[i]).offset().top) - api.int($($floor[i]).height()) / 2)) {
						$elevatorFloor.find("a").eq(i).addClass("active").siblings().removeClass("active");
						return;
					}
				}
			}
		}
	},
	/**
	 * 
	 * 返回顶部
	 * */
	scrollTop: function(a) {
		var b = 1;
		a.click(function() {
			for(var a = document.body.scrollHeight - 1; a && (window.scrollTo(0, a), a > 200 ? (a = a - 2 - b, b += 5) : a -= 2, !(0 > a)););
		})
	},
	/**
	 * 
	 * tab 切换
	 * */
	tabSwitch: function(a) {
		var b = a.current || 1,
			c = a.tab || "tab",
			d = a.tabbar,
			e = a.container;
		d.find("[name='" + c + "'][" + c + "='" + b + "']").addClass("current"), e.find("[is_" + c + "='true'][" + c + "!='" + b + "']").hide(), d.find("[name='" + c + "']").click(function() {
			var a = api.int($(this).attr(c));
			$(this).addClass("current"), d.find("[name='" + c + "'][" + c + "!='" + a + "']").removeClass("current"), e.find("[is_" + c + "='true'][" + c + "!='" + a + "']").hide(0), e.find("[is_" + c + "='true'][" + c + "='" + a + "']").show(50)
		})
	},

	init: function() {
		api.service({
			open: '1',
			data: '[{"name":"%u7F51%u7AD9%u5EFA%u8BBE%u54A8%u8BE2","tel":"020-85586686","qq":"1071322670","crmqq":"","wangwang":"","skype":"","qrcode":"","text":""},{"name":"%u5FAE%u4FE1%u516C%u4F17%u53F7","tel":"","qq":"","crmqq":"","wangwang":"","skype":"","qrcode":"/ow-content/uploads/ow.x4.default/images/qrcode.jpg","text":""}]',
			initType: "list"
		});

		api.floor();

		var $owFloater = $("#ow_floater"),
			$top = $owFloater.find("a.top").hide();

		$(window).scroll(function() {
			var top = parseInt(document.body.scrollTop || document.documentElement.scrollTop);
			if(top < 20) {
				$top.hide(100);
			} else {
				$top.show(100);
			}
		});

		api.scrollTop($top);

		var floors = $('.ow-shop-floor');
		for(var i = 0, len = floors.length; i < len; i++) {
			api.tabSwitch({
				tab: "tab" + (i + 1),
				tabbar: $("#cate" + (i + 1) + "_header"),
				container: $("#floor" + (i + 1) + "_container"),
				current: 1
			});
		}
	}
}

var commonAjax = {
	/**
	 * 登录
	 * */
	login: function(datas, callback) {
		api.ajax('post', '/login', datas, function(res) {
			callback(res);
		});
	}
}